﻿using McHighwayHollowPierUi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace McHighwayHollowPierUi
{
    public class CornerComboItem
    {
        public CornerType Type { get; set; }
        public string Name { get; set; }
    }

    public class HighwayHollowPierViewModel
    {
        public McHighwayHollowPierData Data { get; set; }

        public IList<CornerComboItem> PierCornerComboItems { get; set; } = new List<CornerComboItem>()
        {
            new CornerComboItem { Name="直角", Type = CornerType.Right},
            new CornerComboItem { Name="圆角", Type = CornerType.Cicle},
            new CornerComboItem { Name="倒角", Type = CornerType.Chamfer},
        };

        public IList<CornerComboItem> HollowCornerComboItems { get; set; } = new List<CornerComboItem>()
        {
            new CornerComboItem { Name="直角", Type = CornerType.Right},
            new CornerComboItem { Name="倒角", Type = CornerType.Chamfer},
        };
    }

    public partial class HighwayHollowPierForm : Form
    {
        public HighwayHollowPierViewModel ViewModel { get; set; }

        public HighwayHollowPierForm(HighwayHollowPierViewModel viewModel)
        {
            ViewModel = viewModel;

            InitializeComponent();

            InitBinding();
        }

        /// <summary>
        /// 初始化数据绑定
        /// </summary>
        private void InitBinding()
        {
            tbHeight.DataBindings.Add("Text", ViewModel.Data, "Height");
            tbLength.DataBindings.Add("Text", ViewModel.Data, "Length");
            tbWidth.DataBindings.Add("Text", ViewModel.Data, "Width");
            tbThickness.DataBindings.Add("Text", ViewModel.Data, "Thickness");
            tbGradientWidth.DataBindings.Add("Text", ViewModel.Data, "GradientWidth");
            tbGradientHeight.DataBindings.Add("Text", ViewModel.Data, "GradientHeight");
            tbTopSolidHeight.DataBindings.Add("Text", ViewModel.Data, "TopSolidHeight");
            tbBottomSolidHeight.DataBindings.Add("Text", ViewModel.Data, "BottomSolidHeight");
            lbRadius1.DataBindings.Add("Visible", ViewModel.Data.PierCorner, "CicleVisible");
            tbRadius1.DataBindings.Add("Visible", ViewModel.Data.PierCorner, "CicleVisible");
            tbRadius1.DataBindings.Add("Text", ViewModel.Data.PierCorner, "Radius");
            lbRadius2.DataBindings.Add("Visible", ViewModel.Data.HollowCorner, "CicleVisible");
            tbRadius2.DataBindings.Add("Visible", ViewModel.Data.HollowCorner, "CicleVisible");
            tbRadius2.DataBindings.Add("Text", ViewModel.Data.HollowCorner, "Radius");

            tbCornerLength1.DataBindings.Add("Text", ViewModel.Data.PierCorner, "Length");
            tbCornerHeight1.DataBindings.Add("Text", ViewModel.Data.PierCorner, "Height");
            lbCornerLength1.DataBindings.Add("Visible", ViewModel.Data.PierCorner, "ChamferVisible");
            tbCornerLength1.DataBindings.Add("Visible", ViewModel.Data.PierCorner, "ChamferVisible");
            lbCornerHeight1.DataBindings.Add("Visible", ViewModel.Data.PierCorner, "ChamferVisible");
            tbCornerHeight1.DataBindings.Add("Visible", ViewModel.Data.PierCorner, "ChamferVisible");

            tbCornerLength2.DataBindings.Add("Text", ViewModel.Data.HollowCorner, "Length");
            tbCornerHeight2.DataBindings.Add("Text", ViewModel.Data.HollowCorner, "Height");
            lbCornerLength2.DataBindings.Add("Visible", ViewModel.Data.HollowCorner, "ChamferVisible");
            tbCornerLength2.DataBindings.Add("Visible", ViewModel.Data.HollowCorner, "ChamferVisible");
            lbCornerHeight2.DataBindings.Add("Visible", ViewModel.Data.HollowCorner, "ChamferVisible");
            tbCornerHeight2.DataBindings.Add("Visible", ViewModel.Data.HollowCorner, "ChamferVisible");

            comboBox1.ValueMember = "Type";
            comboBox1.DisplayMember = "Name";
            comboBox1.DataBindings.Add("SelectedValue", ViewModel.Data.PierCorner, "Type", false, DataSourceUpdateMode.OnPropertyChanged);
            comboBox1.DataSource = new BindingSource(ViewModel.PierCornerComboItems, null);

            comboBox2.ValueMember = "Type";
            comboBox2.DisplayMember = "Name";
            comboBox2.DataBindings.Add("SelectedValue", ViewModel.Data.HollowCorner, "Type", false, DataSourceUpdateMode.OnPropertyChanged);
            comboBox2.DataSource = new BindingSource(ViewModel.HollowCornerComboItems, null);
        }

        private void draw_Click(object sender, EventArgs e)
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(ViewModel.Data);

            McHighwayHollowPierCli.IMcHighwayHollowPierCli.getInstance().drawHighwayHollowPier(json);
        }
    }
}
